﻿using System.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraBars;

namespace unitlControl
{
    public class ComboUtils
    {
        private readonly string _giatri;
        private readonly string _hienthi;

        public ComboUtils(string giatri,string hienthi)
        {
            _giatri = giatri;
            _hienthi = hienthi;
        }
             
        public string Giatri
        {
            get { return _giatri; }
        }

        public string Hienthi
        {
            get { return _hienthi; }
        } 
     
        //đổ dữ liệu vào comboboxx
        public static void SetDataSuorce(ComboBoxEdit combo, DataTable dt, string giatri, string hienthi, int selectIndex)
        {
            if (dt == null)
                return;
            combo.Properties.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                combo.Properties.Items.Add(new ComboUtils(dr[giatri].ToString(), 
                                                           dr[hienthi].ToString()));
            }
            combo.SelectedIndex = selectIndex;
            combo.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
        }
 
        public override string ToString()
        {
            return _hienthi;
        }


        //lấy giá trị của combo
        public static string LayGT_Combobox(ComboBoxEdit combo)
        {
            if (combo.SelectedItem == null)
                return null;
            return ((ComboUtils)combo.SelectedItem).Giatri;
        }

        // lấy giá trị hiển thị
        public static string LayGTHienthi(ComboBoxEdit combo)
        {
            if (combo.SelectedItem == null)
                return null;
            return ((ComboUtils)combo.SelectedItem)._hienthi;
        }
        //Selected Combobox
        public static void SelectedCombox(ComboBoxEdit combo,string giatri)
        {
            foreach (object item in combo.Properties.Items)
            { 
                if(((ComboUtils) item).Giatri == giatri)
                {
                    combo.SelectedItem = item;
                }

            }
        }

        public static void SelectedItem(ComboBoxEdit comb, string svalue)
        {
            foreach (object item in comb.Properties.Items)
            {
                if (((ComboUtils)item).Hienthi== svalue)
                    comb.SelectedItem = item;
            }
        }

        //du lieu R
        public static DevExpress.XtraEditors.Repository.RepositoryItemComboBox SetDataSuorceR(DevExpress.XtraEditors.Repository.RepositoryItemComboBox combo, DataTable dt, string giatri, string hienthi, int selectIndex)
        {
            combo = new DevExpress.XtraEditors.Repository.RepositoryItemComboBox();
            if (dt == null)
                return combo;
            combo.Items.Clear();
            foreach (DataRow dr in dt.Rows)
            {
                combo.Items.Add(new ComboUtils(dr[giatri].ToString(),
                                                           dr[hienthi].ToString()));
            }
            
            combo.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            return combo;
        }
      

     
   }
}
